<?php
	require "lib.php";

	$type = $_GET['type'];

	switch ($type) {
		case 'login':
			doLogin();
			break;
		case 'register':
			doRegist();
			break;
		case 'sendweibo':
			doSend();
			break;
		case 'layout':
			doLayout();
		break;
		case 'follow':
			doFollow();
		break;
		default:
			//exit('type 不存在');
			break;
	}


	// 用户登陆
	function doLogin() {
		$username = $_POST['username'];
		$password = $_POST['password'];
		if(empty($username) || empty($password)) {
			//header('location: login.php');
			// header("refresh:2,url=login.php");
			// echo "参数不足";
			returnTip('login.php',NO_PARAM);
		}
		$redis = connect_redis();
		$useid = $redis->get("user:userid:username".$username); 
		if(empty($useid)) {
			returnTip('login.php',UNAME_ERROR);
		}

		$passwords = $redis->get("user:password:userid".$useid);
		if($passwords != $password) {
			returnTip('login.php',PWD_ERROR);
		}

		setcookie('username',$username);
		setcookie('userid',$useid);
		// $_COOKIE['username'] = $username;
		// $_COOKIE['userid'] = $userid;
		returnTip('index.php',LOGIN_SUCCESS);
	}

	// 用户注册
	function doRegist() {
		$username = $_POST['username'];
		$password = $_POST['password'];
		$pwd_agent = $_POST['pwd_agent'];

		if(empty($username) || empty($password) || empty($pwd_agent)) {
			returnTip('login.php',NO_PARAM);
		}
		if($password != $pwd_agent) {
			returnTip('login.php',PWD_INCONSISTEN);
		}

		$redis = connect_redis();
		// 判断是否存在用户名
		$is_exist = $redis->get("user:userid:username".$username);
		if(!empty($is_exist)) {
			returnTip('login.php',USER_EXIS);
		}

		$userid = $redis->incr("golbal:userid");								// 自增用户id
		$res1 = $redis->set("user:username:userid".$userid,$username);			// 存用户名
		$res2 = $redis->set("user:userid:username".$username,$userid);			// 存用户id
		$res3 = $redis->set("user:password:userid".$userid,$password);          // 用户密码

		if($res1 && $res2 && $res3) {
			setcookie("userid",$userid);
			setcookie("username",$username);

			// $_COOKIE['userid'] = $userid;
			// $_COOKIE['username'] = $username;
			// 加入用户队列中
			$res = $redis->lpush("users",$userid);
			if($res) {
				returnTip('index.php',OPACT_SUCCESS);
			}
		}else {
			returnTip('login.php',FAILE);
		}
 	}

 	// 发布微博
 	function doSend() {
 		$userid = $_POST['userid'];
 		$content = $_POST['content'];

 		if(empty($userid) || empty($content)) {
 			returnTip('index.php',NO_PARAM);
 		}

 		$redis = connect_redis();
 		$weibo_id = $redis->incr("global:weibo");

 		// 将微博信息存入哈希
 		$weibo_arr = array(
 			"userid" => $userid,
 			"content" => $content,
 			"create_time" => time(),
 		);
 		$weibo_res = $redis->hMset("weibo:data:weiboid".$weibo_id,$weibo_arr);
 		// 将微博id管理到对应用户下(有序集合)
 		if($weibo_res) {
 			$res = $redis->zadd("userweibo:list:userid".$userid,time(),$weibo_id);

 			if($res) {
 				returnTip('index.php',OPACT_SUCCESS);
 			}
 		}
 	}

 	// 退出登陆
 	function doLayout() {
 		setcookie("userid",null);
 		setcookie("username",null);
 		returnTip('login.php',OPACT_SUCCESS);
 	}

 	// 关注/取消关注 用户
 	function doFollow() {
 		$stas = $_GET['stas'];
 		$target_uid = $_GET['userid'];
 		$userid = $_COOKIE['userid'];

 		if(empty($stas) || empty($target_uid) || empty($userid)) {
 			returnTip($_SERVER['HTTP_REFERER'],NO_PARAM);
 		}

 		$redis = connect_redis();

 		if($stas=="follow") {
 			// 将目标用户存入我关注集合中，并把我放到对方粉丝的集合中
	 		$res1 = $redis->sAdd("follows:userid".$userid,$target_uid);
	 		$res2 = $redis->sAdd("fans:userid".$target_uid,$userid);
 		}else {
 			$res1 = $redis->sPop("follows:userid".$userid,$target_uid);
 			$res2 = $redis->sPop("fans:userid".$target_uid,$userid);
 		}
 		
 		if($res1 && $res2) {
 			returnTip($_SERVER['HTTP_REFERER'],OPACT_SUCCESS);
 		}else {
 			returnTip($_SERVER['HTTP_REFERER'],FAILE);
 		}
 	}

?>